package com.example.yuan0605.test;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yml.R;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private ListView lv;
    private MyPageAdapter adapter;
    List<User> list;
    Users db;
    boolean flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_main_2);
        lv = findViewById(R.id.lv);
        Intent intent=getIntent();
        flag=intent.getBooleanExtra("issex",false);
        if (flag){
            db=new Users(this,2);
        }else {
            db=new Users(this,1);
        }
        list =new ArrayList<>();
        initdata();
        adapter=new MyPageAdapter(list,this);
        lv.setAdapter(adapter);
    }
    public void initdata(){
        SQLiteDatabase dldb = db.getReadableDatabase();
        Cursor cursor = dldb.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()){
            User user=new User();
            user.setUserId(cursor.getInt(cursor.getColumnIndex("userId")));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
            if (flag){
                user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
            }
            list.add(user);
        }
        cursor.close();
        dldb.close();
    }
}
